import query from './atom/db/query';
import count from './atom/db/count';
import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	filter: {};
	sort: {};
	page_size: number;
	page_no: number;
}

// 添加数据表中的所有字段和类型

interface interviewInfo extends IBaseTable {
	_id: string; no: string; resume_name: string; sex: string; age: string; docking_date: number; post_id: string; exec_plan_no: string; work_plan: string; score: string; written_score: string; resume_interview_comment: string; reexamine_com: string; staus: string; audit_opinion: string; auditname: string; auditdate: number; result: string; accessory_url: string;
}

function format(date: number) {
	if (date === null) {
		return '';
	}
	try {
		const _date = new Date(parseFloat(date + ''));
		if (_date) {
			return _date.getFullYear() + '-' + _format(_date.getMonth() + 1) + '-' + _format(_date.getDate());
		}
		return '';
	} catch (error) {
		return '';
	}
}

function _format(d: number) {
	if (d < 10) {
		return '0' + d;
	} else {
		return d;
	}
}

async function atom(msg: Message) {
	const systemid = '6051';
	const { exec, prepare } = query(action_id, session_id, spaceid, systemid);
	const asc: string[] = [];
	const desc: string[] = [];
	if (msg.sort) {
		const key = Object.keys(msg.sort)[0];
		if (msg.sort[key] === -1) {
			desc.push(key);
		} else {
			asc.push(key);
		}
	}
	// 证照信息
	prepare('interview_info', [
		"_id", "no", "resume_name", "sex", "age", "docking_date", "post_id", "exec_plan_no", "work_plan", "score", "written_score", "resume_interview_comment", "reexamine_com", "staus", "audit_opinion", "auditname", "auditdate", "result", "accessory_url"
	], msg.filter, msg.page_size, msg.page_no, asc, desc);
	const [data] = await exec();

	data.forEach((item: interviewInfo) => {
		item['docking_date_str'] = format(item.docking_date);
		item['auditdate_str'] = format(item.auditdate);
	});

	const c = count(action_id, session_id, spaceid, systemid);
	c.prepare('interview_info', msg.filter);
	const [t] = await c.exec();

	return { dataArray: data, count: t };
}